package com.tyk.bookstore.back.logistics.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.model.type.SuggestionQuery;
import com.tyk.bookstore.back.logistics.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.logistics.manager.excel.model.ExpressCompanyExcelVo;
import com.tyk.bookstore.back.logistics.model.query.ExpressCompanyAddQuery;
import com.tyk.bookstore.back.logistics.model.query.ExpressCompanyPageQuery;
import com.tyk.bookstore.back.logistics.model.query.ExpressCompanyUpdateQuery;
import com.tyk.bookstore.back.logistics.model.vo.ExpressCompanyPageVo;
import com.tyk.bookstore.back.logistics.model.vo.ExpressCompanyVo;
import com.tyk.bookstore.back.logistics.service.ExpressCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;


/**
 * @author tyk
 * @description 物流公司管理
 */
@RequestMapping({"api/logistics/company"})
@Tag(name = "物流管理/物流公司管理", description = "物流公司管理")
@RestController
@RequiredArgsConstructor
public class ExpressCompanyController {

    private final ExpressCompanyService expressCompanyService;
    private final ExcelManager<ExpressCompanyExcelVo> excelManager;


    @Operation(summary = "查询物流公司详情", description = "查询物流公司详情")
    @SaCheckPermission("logistics:company:get")
    @GetMapping({"{id}"})
    public R<ExpressCompanyVo> getById(
            @Schema(description = "物流公司ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(
                expressCompanyService.getById(id)
        );
    }


    @Operation(summary = "分页查询物流公司", description = "分页查询物流公司")
    @SaCheckPermission("logistics:company:page")
    @GetMapping({"page"})
    public R<ExpressCompanyPageVo> getPage(
            @Validated @ParameterObject ExpressCompanyPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(ExpressCompanyPageVo.class)) : R.ok(expressCompanyService.getPage(query));
    }


    @Operation(summary = "查询物流公司搜索建议列表", description = "查询物流公司搜索建议列表")
    @SaCheckPermission("logistics:company:suggest")
    @GetMapping({"suggest"})
    public R<List<BinVo>> getSuggestionList(@Validated @ParameterObject SuggestionQuery query) {
        return R.ok(expressCompanyService.getSuggestionList(query));
    }


    @Operation(summary = "添加物流公司", description = "添加物流公司")
    @SaCheckPermission("logistics:company:add")
    @PostMapping
    public R<Object> add(@Validated @RequestBody ExpressCompanyAddQuery query) {
        expressCompanyService.add(query);
        return R.ok();
    }


    @Operation(summary = "更新物流公司", description = "更新物流公司")
    @SaCheckPermission("logistics:company:update")
    @PutMapping
    public R<Object> updateById(@Validated @RequestBody ExpressCompanyUpdateQuery query) {
        expressCompanyService.updateById(query);
        return R.ok();
    }


    @Operation(summary = "删除物流公司", description = "删除物流公司")
    @SaCheckPermission("logistics:company:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @Schema(description = "物流公司ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        expressCompanyService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "批量删除物流公司", description = "批量删除物流公司")
    @SaCheckPermission("logistics:company:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(@NotNull @NotEmpty Set<Long> idList) {
        expressCompanyService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("logistics:company:excel:download")
    @GetMapping("excel")
    public void downloadExcel(ExpressCompanyPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                expressCompanyService.getExpressCompanyExcelVoList(query),
                ExcelConstant.EXPRESS_COMPANY_FILE_NAME,
                ExcelConstant.EXPRESS_COMPANY_SHEET,
                resp
        );
    }


    @Operation(summary = "导入Excel", description = "导入Excel")
    @SaCheckPermission("logistics:company:excel:upload")
    @PostMapping("excel")
    public R<Object> uploadExcel(MultipartFile file) {
        excelManager.save(
                file,
                expressCompanyService::saveExpressCompanyExcelVoList
        );
        return R.ok();
    }


    @Operation(summary = "下载Excel模板", description = "下载Excel模板")
//    @SaCheckPermission("logistics:company:excel:template")
    @GetMapping("excel/template")
    public void excelTemplate(HttpServletResponse resp) {
        excelManager.template(
                ExcelConstant.EXPRESS_COMPANY_TEMPLATE,
                List.of("简称", "全称", "收费[单位：元/千克]", "描述"),
                resp
        );
    }

}
